// pages/master/userSettings/userSettings.js
import { uploadFile } from '../../../tool/HttpRequest'
import { updateUser, gradelist } from '../../../apis/index'
import { Toast, GetStorage, SetStorage, ClearStorage } from '../../../tool/wx.public'
var that
Page({

    /**
     * 页面的初始数据
     */
    data: {
        // current: 2019,
        SexSelection: [{ label: '男', value: 1 }, { label: '女', value: 2 }, { label: '未知', value: 0 }],
        nickname: '',//修改呢称
        ConfirmShowName: false,//是否显示呢称
        userData: {},
        phaseName: '',
        phase: []// 学习阶段数据
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        // that = this

        gradelist().then(res => {
            const userData = GetStorage('userData')
            console.log(res, userData.grade);

            let getName = res.find(e => e.grade_id == userData.grade)
            console.log(getName);
            // phaseName
            this.setData({
                phase: res,
                phaseName: getName?.name || '还未设置年级'
            })
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {
        // that.picker = that.selectComponent("#custom-picker")
        // that.picker.showDialog(that.data.current)
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        const userData = GetStorage('userData')
        this.setData({
            userData: userData,
            nickname: userData.nickname
        })
    },


    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },
    showpicker() {
        that.picker.showDialog(1) // 打开picker选择器，指定回显
    },
    _lefttap() {
        wx.showToast({
            title: '左边按钮被点击',
            icon: 'none'
        })
    },
    _righttap(e) {
        var year = e.detail.year
        wx.showToast({
            title: '右边按钮被点击,选择的值为：' + year,
            icon: 'none'
        })
        that.setData({
            current: year
        })
    },
    _cancel() {
        wx.showToast({
            title: 'picker被取消',
            icon: 'none'
        })
    },
    // 修改呢称
    onInput: function (event) {
        var value = event.detail.value;
        if (value.length <= 7) {
            this.setData({
                nickname: value
            })
        }

    },
    // 关闭呢称输入
    cancelConfirm() {
        this.setData({
            ConfirmShowName: false
        })
    },
    //选择头像上传
    chooseMedia() {
        Toast('请在APP修改~')
        return
        wx.chooseMedia({
            count: 1,
            mediaType: ['image'],
            sourceType: ['album', 'camera'],
            sizeType: ['original', 'compressed'],
            camera: 'back',
            success(res) {
                if (res.errMsg === 'chooseMedia:ok') {
                    const [file] = res.tempFiles
                    console.log(file.tempFilePath)
                    console.log(file.size)
                    console.log(res)

                    uploadFile(file.tempFilePath).then(res => {
                        console.log(res)
                    }).catch(err => {
                        Toast('修改头像上传失败')
                    })
                }

            }
        })
    },
    showModule(e) {
        const { key } = e.currentTarget.dataset
        this.setData({
            [key]: !this.data[key]
        })
    },
    // 内容复制
    ClipidId(e) {
        wx.setClipboardData({
            data: e.currentTarget.dataset.userid,
            success(res) {
                //   wx.getClipboardData({
                //     success (res) {
                //       console.log(res.data) // data
                //     }
                //   })
            }
        })
    },
    bindTimeChange: function (e) {
        console.log('picker发送选择改变，携带值为', e.detail.value)
        this.editUser('birthday', e.detail.value)
    },
    editsex(e) {
        const gender = this.data.SexSelection[e.detail.value].value
        this.editUser('gender', gender)
    },
    editgrade(e) { // 修改年级
        console.log('picker发送选择改变，携带值为', e.detail.value)
        const id = this.data.phase[e.detail.value].grade_id
        let getName = this.data.phase.find(e => e.grade_id == id)
        this.setData({
            phaseName: getName.name
        })
        this.editUser('grade_id', id)
    },
    editUser(key, value) {
        updateUser({ [key]: value }).then(res => {
            this.setUserData(key, value)
            Toast('修改成功')
        }).catch(err => {
            Toast(err.mes)
        })
    },
    // 修改缓存数据
    setUserData(key, data) {
        let user = GetStorage('userData')
        user[key] = data
        SetStorage('userData', user)
        this.setData({
            userData: user
        })
    },
    // 退出登录
    quit() {
        ClearStorage()
        wx.switchTab({
            url: '/pages/master/my_index/my_index',
        })
    }
})